package com.sdwz.library.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdwz.commonutil.Result;
import com.sdwz.library.entity.BookInfo;
import com.sdwz.library.entity.vo.BookInfoVo;
import com.sdwz.library.service.BookCategoryService;
import com.sdwz.library.service.BookInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/admin/library/book")
public class BookInfoController {
    @Autowired
    BookInfoService bookInfoService;
    @Autowired
    BookCategoryService bookCategoryService;


    @GetMapping("/get/{id}")
    public Result getBookById(@PathVariable String id){
        BookInfo bookInfo = bookInfoService.getById(id);
        if (bookInfo == null){
            return Result.fail().message("查询书籍不存在");
        }

        return Result.ok(bookInfo);
    }

    @GetMapping("/client/{id}")
    public BookInfo getBook(@PathVariable String id){
        BookInfo bookInfo = bookInfoService.getById(id);
        return bookInfo;
    }


    /**
     * 多条件分页查询图书
     * @param current
     * @param limit
     * @param bookInfoVo
     * @return
     */
    @GetMapping("/list/{current}/{limit}")
    public Result getBookList(@PathVariable long current,
                              @PathVariable long limit,
                              BookInfoVo bookInfoVo){
        Page<BookInfo> page = bookInfoService.selectBookPage(current,limit,bookInfoVo);
        return Result.ok(page);
    }

    @PostMapping("/save")
    public Result saveBookInfo(@RequestBody BookInfo bookInfo){
        bookInfo.setLoanableQuantity(bookInfo.getCollectionQuantity());
        boolean flag = bookInfoService.save(bookInfo);
        if (!flag){
            return Result.fail().message("添加失败");
        }
        return Result.ok();
    }

    /**
     * excel数据导入
     * @param file
     * @return
     */
    @PostMapping("/importByExcel")
    public Result saveByExcel(MultipartFile file){
        bookInfoService.importExcelData(file);
        return Result.ok();
    }

    /**
     * excel模板导出
     * @param response
     * @return
     */
    @GetMapping("/exportTemplate")
    public void exportTemplate(HttpServletResponse response){
        bookInfoService.exportTemplate(response);

    }

    @DeleteMapping("/{id}")
    public Result removeBook(@PathVariable String id){
        boolean flag = bookInfoService.removeById(id);
        if (!flag){
            return Result.fail().message("删除失败");
        }
        return Result.ok();
    }

    @DeleteMapping("/batchRemove")
    public Result batchRemoveBook(@RequestBody List<String> idList){
        boolean flag = bookInfoService.removeByIds(idList);
        if (!flag){
            return Result.fail().message("删除失败");
        }
        return Result.ok();
    }

    @PostMapping("/update")
    public Result updateBookInfo(@RequestBody BookInfo bookInfo){
        boolean flag = bookInfoService.updateById(bookInfo);
        if (!flag){
            return Result.fail().message("修改失败");
        }
        return Result.ok();
    }
}
